/**
 * 医疗服务质询初始化模块，提供医疗服务编辑页面初始化组件功能
 * 
 * @Date: 2016-1-25
 * @author: Jackson wang
 * @version: 1.0
 * @since: 1.0
 */

define(["jquery","sysmanMain","msgServ"],
		function($,sysmanMain,msgServ) {
			
		'use strict';
		var _typeUrl = '/dictionary/getTree.action',_rootpath
		
		var public_method ={
			
			/**
			 * 初始化新增页面
			 * @param _options.record 列表选中行
			 * @param _options.form 页面form对象
			 * @param _options.rootId	页面根节点id
			 * @param _options.flowStateSelector	流程状态字段选择器
			 */
			initElement:function(_options){
				
					
			},
			
			/**
			 * @param _options.rootId
			 * @param _options.conditionSelector
			 * @param _options.
			 */
			initList:function(_options){
				private_method.initCondition(_options)
			},
			
			/**
			 * 初始化流程业务表单字段
			 * @param _options.fieldConfig	json格式字段配置
			 * @param _options.fieldStatus	字段状态
			 * @param _options.window_id	编辑窗口id
			 */
			initField:function(_options){
				
				//字段状态
				var data = _options.fieldStatus;
				
				
				
				if(_options.fieldConfig && _options.window_id){
					//获取字段配置
					var fieldConfig = _options.fieldConfig
					var _window_id = _options.window_id
					var 
					//editable fields
					editFields,					
					
					//required fields
					requiredFields					
					
					//判断并获取字段配置
					if(data && data.editFilter){
						if(data.editFilter.enabled){
							editFields = data.editFilter.enabled.split(',');
						}else{
							editFields = data.editFilter.split(',');
						}
						if(editFields && editFields.length>0){
							for(var i=0;i<editFields.length;i++){
								var _e = editFields[i]
								if(fieldConfig[_e]){
									fieldConfig[_e].readonly = false;
									fieldConfig[_e].editable = true;
									fieldConfig[_e].targetElement = $('#' +_window_id + ' #' +_e);
								}
								
								private_method.reCreateField(fieldConfig[_e]);
							}
						}
					}
					if(data && data.requiredFilter){
						requiredFields = data.requiredFilter.split(',');
						if(requiredFields && requiredFields.length>0){
							for(var i=0;i<requiredFields.length;i++){
								var _r = requiredFields[i]
								if(fieldConfig[_r]){
									fieldConfig[_r].readonly = false;
									fieldConfig[_r].editable = true;
									fieldConfig[_r].required = true;
									fieldConfig[_r].targetElement = $('#' +_window_id + ' #' +_r);
								}
								
								private_method.reCreateField(fieldConfig[_r]);
							}
						}
					}
				
				}		
			}			
			
			
		}
		
		var private_method = {
			
			/**
			 * @param _options.targetElement	目标对象
			 * @param _options.fieldType		对象类型
			 * @param _options.fieldConfig		对象配置
			 */
			reCreateField:function(_options){
				
				if(_options){
					
					switch(_options.fieldType){
						case 'textbox':
							$(_options.targetElement).textbox(_options);
							break;
						case 'combobox':
							$(_options.targetElement).combobox(_options);
							break;
						case 'datebox':
							$(_options.targetElement).datebox(_options);
							break;
						case 'datetimebox':
							$(_options.targetElement).datetimebox(_options);
							break;
						default:
							$(_options.targetElement).textbox(_options);
					}
				}
			},
			/**
			 * 初始化条件查询框
			 * @param _options.rootId 窗口根节点id
			 * @param _options.conditionSelector 条件框节点选择器
			 * @param _options.callback
			 */
			initCondition:function(_options){
				if(window.console){
					console.info('recipel init loaded and starting...')
				}
				
				_rootpath = '/'+sysmanMain.getRootPath()
				var defaultconfig ={
					rootId:null,
					conditionSelector:'form.condition-form',
					flowStateSelector:'flow_state'
				}
				$.extend(defaultconfig,_options)
				if(defaultconfig.rootId && defaultconfig.conditionSelector){
					//初始化流程状态
					var flowStateEl = $('#'+defaultconfig.rootId +' ' +defaultconfig.conditionSelector +' #'+defaultconfig.flowStateSelector)
					
					if(flowStateEl.length>0){
					$(flowStateEl).combobox({
						textField:'name',
						valueField:'bizcode',
						url:_rootpath + _typeUrl+'?id=' + 89,
						onSelect:function(record){
							var _code = record.bizcode;
							
							var _btn = $('#'+_options.rootId + ' td.list-btn a.function-btn')
							if(_btn.length>0){
								if(_code.indexOf('canOperate')>=0){
									//disable button								
									$(_btn).linkbutton('enable');
								}else{
									//enable button
									$(_btn).linkbutton('disable');
								}								
							}
							var _grid = $('#'+_options.rootId + ' table.dg-list')
							if(_grid.length>0){
								$(_grid).datagrid('loadData',{"total":0,"rows":[]})
							}
						}
						})
						
					}
				}
			}
		}
		
		
		return public_method;
			
})